package com.landleaf.smarthome.net.mqtt;

import com.landleaf.smarthome.MockData;
import com.landleaf.smarthome.model.DeviceAttribute;
import com.landleaf.smarthome.model.DeviceModel;
import com.landleaf.smarthome.model.SceneModel;
import com.landleaf.smarthome.model.SecurityModel;
import com.landleaf.smarthome.net.mqtt.bean.MqttDataModel;
import com.landleaf.smarthome.net.mqtt.bean.MqttHeaderModel;
import com.landleaf.smarthome.net.mqtt.bean.MqttMsgBodyModel;
import com.landleaf.smarthome.net.mqtt.bean.MqttPayloadModel;
import com.landleaf.smarthome.util.JsonUtils;
import com.landleaf.smarthome.util.SpUtils;

import java.util.List;

/**
 * When I wrote this, only God and I understood what I was doing
 * Now, God only knows
 * Created by 海航
 * 2020/9/27
 * 功能描述:
 */
public class MqttExecutor {
    private static volatile MqttExecutor singleton = null;

    private MqttExecutor() {
    }

    public static MqttExecutor getInstance() {
        if (singleton == null) {
            synchronized (MqttExecutor.class) {
                if (singleton == null) {
                    singleton = new MqttExecutor();
                }
            }
        }
        return singleton;
    }

    public void connect() {
        MqttClient.getInstance().connect();
    }

    public void disConnect() {
        MqttClient.getInstance().disConnect();
    }

    /**
     * 返回公共的响应
     */
    public void sendCommonReturnMsg(MqttMsgBodyModel requestMsg) {
        if (requestMsg.getHeader().getAckCode() == 1) {
            return;
        } else {
            requestMsg.getHeader().setAckCode(1);
            requestMsg.getPayload().setCode(200);
            requestMsg.getPayload().setMessage("success");
            sedMsg(requestMsg);
        }
    }

    /**
     * 发送设备读取的数据
     */
    public void sendDeviceReadMsg(MqttMsgBodyModel<DeviceAttribute> requestMsg, DeviceModel deviceModel) {
        requestMsg.getPayload().getData().setItems(deviceModel.getAtts());
        sendCommonReturnMsg(requestMsg);
    }

    /**
     * 发送设备更新的数据
     */
    public void sendDeviceUpdateMsg(DeviceModel deviceModel) {
        deviceModel=MockData.getDeviceModel();
        MqttMsgBodyModel model = getCommonMqttRequestMsgModel();
        model.getHeader().setAckCode(1);
        model.getHeader().setName(MqttConst.METHOD_DEVICE_UPDATE);
        MqttDataModel data = model.getPayload().getData();
        data.setItems(deviceModel.getAtts());
        data.setProductCode(deviceModel.getProductCode());
        data.setDeviceSn(deviceModel.getDeviceSn());
        sedMsg(model);
    }

    /**
     * 发送报警信息读取的数据
     */
    public void sendSecurityAlarmMsg(List<SecurityModel> securityModels) {
        MqttMsgBodyModel model = getCommonMqttRequestMsgModel();
        model.getHeader().setAckCode(1);
        model.getHeader().setName(MqttConst.METHOD_SECURITY_ALARM);
        model.getPayload().getData().setItems(MockData.getSecurityList());
        sedMsg(model);
    }
    /**
     * 大屏控制场景上报数据
     */
    public void sendSceneUpdateMsg(SceneModel sceneModel) {
        MqttMsgBodyModel model = getCommonMqttRequestMsgModel();
        model.getHeader().setAckCode(1);
        model.getHeader().setName(MqttConst.METHOD_SCENE_UPDATE);
        model.getPayload().getData().setSceneId("1");
        sedMsg(model);
    }

    public void sedMsg(MqttMsgBodyModel msg) {
        MqttClient.getInstance().publishMessage(JsonUtils.objToJson(msg));
    }

    /**
     * 构件一个公共属性的请求体
     *
     * @return
     */
    private MqttMsgBodyModel getCommonMqttRequestMsgModel() {
        MqttMsgBodyModel mqttMsgBodyModel = new MqttMsgBodyModel();
        MqttHeaderModel header = new MqttHeaderModel();
        header.setAckCode(0);
        header.setMessageId(String.valueOf(getNewMsgId()));
//        header.setScreenMac(DeviceInfoUtil.getDeviceId(App.getInstance()));
        header.setScreenMac("aa:bb:cc:dd:ee:ff");
        mqttMsgBodyModel.setHeader(header);
        MqttPayloadModel payload = new MqttPayloadModel();
        payload.setData(new MqttDataModel());
        mqttMsgBodyModel.setPayload(payload);
        return mqttMsgBodyModel;
    }


    private int getNewMsgId() {
        int msgId = SpUtils.readInt("msgID", 1);
        if (msgId >= 65500) {
            msgId = 1;
        }
        SpUtils.save("msgID", msgId + 1);
        return msgId;
    }
}
